//
// Created by 徐套 on 2023/9/29.
//

#ifndef CAR_CLOUD_SERVER_MAIN_SERVER_H
#define CAR_CLOUD_SERVER_MAIN_SERVER_H

#include <memory>
#include <string>
#include "server.h"
#include "upper_computer_server.h"
#include <vector>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include "common/clock.hpp"
#include "processor/car_contral.h"



class main_server {
public:

    main_server() {};

    main_server(int worker, int listen, char *local_ip) {
        max_server = worker;
        listen_port = listen;
        ip = local_ip;
        start = true;

        m_listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        time_ = Clock_time();
        car = new car_contral();
    }

    ~main_server() {
        start = false;
        delete(car);
    }

    // 启动服务器
    void start_server();

    bool create_new_car_client();

    void lock_in_time();

    // 开始监听连接的服务器并分配
    void listen_client();

    bool send_msg_to_upper_by_udp();

    // 通过tcp连接给上位机发送数据
    void send_msg_to_upper_by_tcp(std::string msg);

    void car_server_is_alive();

private:
    char *ip;
    int max_server;
    car_contral *car;
    std::shared_ptr<upper_computer_server> upper_server;
    int listen_port;
    int m_listen_socket;
    bool start;
    Clock_time time_;
};


#endif //CAR_CLOUD_SERVER_MAIN_SERVER_H
